<?php

namespace App\depository;

use App\Enum\CommonEnum;
use App\Enum\UsersEnum;
use App\Models\SubTeamUserModel;
use App\Models\SubUserHasRolesModel;
use App\Models\UserModel;

class UserDep
{
    public $model;

    public function __construct()
    {
        $this->model = new UserModel();
    }

    public function firstById($id){
        $res = $this->model->where('id',$id)->first();
        return $res;
    }

    public function list($param)
    {
        $res = $this->model
            ->when(!empty($param['username']), function ($query) use ($param) {
                return $query->where("name", 'like', "%{$param['username']}%");
            })
            ->when(!empty($param['nickname']), function ($query) use ($param) {
                return $query->where("nickname", 'like', "%{$param['nickname']}%");
            })
            ->when(!empty($param['role_id']), function ($query) use ($param) {
                return $query->where("role_id", $param['role_id']);
            })
//            ->when(!empty($param['expire_at'][0]),function($query) use($param){
//                return $query->where("expire_at",">=",$param['expire_at'][0]);
//            })
//            ->when(!empty($param['expire_at'][1]),function($query) use($param){
//                return $query->where("expire_at","<=",date("Y-m-d",strtotime("+1 day",strtotime($param['expire_at'][1]))));
//            })
            ->where('is_del', CommonEnum::NO)
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }


    public function add($data)
    {
        $res = $this->model->insert($data);
        return $res;
    }

    public function upd($id, $data)
    {
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function del($id)
    {
        $res = $this->model->where('id', $id)->update(['is_del' => CommonEnum::YES]);
        return $res;
    }

    public function firstByUsername($username)
    {
        $res = $this->model->where('name', $username)->first();
        return $res;
    }

    public function firstLikeUsername($username)
    {
        $res = $this->model->where('name','like', "%{$username}%")->first();
        return $res;
    }


    public function allOk(){
        $res = $this->model->where('status',UsersEnum::STATUS_YES)->where('is_del',CommonEnum::NO)->get();
        return $res;
    }

    public function subUserList($teamId,$param){

        $subTeamUserTable = (new SubTeamUserModel())->getTable();
        $currentTable = $this->model->getTable();
        $subUserHasRole = (new SubUserHasRolesModel())->getTable();

        $res = $this->model->leftJoin($subTeamUserTable,"{$currentTable}.id",'=',"{$subTeamUserTable}.user_id")
                            ->select(["{$currentTable}.id","{$currentTable}.name","{$currentTable}.nickname","{$currentTable}.created_at","{$currentTable}.email","{$currentTable}.status","{$currentTable}.password"])
                            ->when(!empty($param['username']), function ($query) use ($param,$currentTable) {
                                return $query->where("{$currentTable}.name", 'like', "%{$param['username']}%");
                            })
                            ->when(!empty($param['nickname']), function ($query) use ($param,$currentTable) {
                                return $query->where("{$currentTable}.nickname", 'like', "%{$param['nickname']}%");
                            })
                            ->when(!empty($param['role_id']), function ($query) use ($param,$currentTable,$subUserHasRole,$teamId) {
//                                return $query->where("{$currentTable}.role_id", $param['role_id']);
                                $query->whereIn("{$currentTable}.id",function ($query) use ($param,$currentTable,$subUserHasRole,$teamId){
                                    return $query->select(['user_id'])->from($subUserHasRole)->where("{$subUserHasRole}.team_id", $teamId)->where("role_id",$param['role_id']);
                                });
                            })
                            ->where("{$subTeamUserTable}.team_id",$teamId)
                            ->where("{$currentTable}.is_del",CommonEnum::NO)
                            ->orderBy("{$currentTable}.id", 'desc')
                            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);

        return $res;
    }

    public function firstByEmail($email){
        $res = $this->model->where('email',$email)->where('is_del',CommonEnum::NO)->first();
        return $res;
    }

    public function getByEmail($email){
        $res = $this->model->where('email',$email)->where('is_del',CommonEnum::NO)->get();
        return $res;
    }
}
